<?php

defined('BY_JYA') or exit('error');
/*
	游戏营销
*/
$_RQ['op']=$_RQ['op']?:'edit';
$return['types']=$this->canyu_types;
$return['rewardtype']=$this->rewardtype;
$return['get_types']=$this->get_types;
$return['times']=$this->times;
$return['addtimes_types']=$this->addtimes_types;
$return['prizeType']=$this->prizeType;
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$redfee_rate=0;
		if($post['reward']){
			foreach($post['reward'] as &$row){
				$row['redfee_rate']=round($row['redfee_rate']*1,2);
				$redfee_rate+=$row['redfee_rate'];
			}
		}
		if($redfee_rate && $redfee_rate!=100){
			//exi('奖金池%之和必须等于100，请检查并重新设置！','error');
		}
		$post['pluginset']['red_shareset']=$post['red_shareset'];
		$data=array(
			'title'=>trim($post['title']),
			'game_id'=>intval($post['game_id']),
			'type'=>intval($post['type']),
			'price'=>round($post['price'],2),
			'goodsid'=>intval($post['goodsid']),
			'time'=>intval($post['time']),
			'status'=>intval($post['status']),
			'sendtitle'=>trim($post['sendtitle']),
			'sendimage'=>trim($post['sendimage']),
			'senddesc'=>trim($post['senddesc']),
			'pluginset'=>ijson_encode($post['pluginset']),
			'addtimes'=>ijson_encode($post['addtimes']),
			'reward'=>ijson_encode($post['reward']),
			'delredfee'=>trim($post['delredfee']),
			'delredtimes'=>trim($post['delredtimes']),
		);
		$id=intval($post['id']);
		if($id){
			pdo_update('jy_weishop_game',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			$data['action']='jy_robshop/game/';
			$data['plugin_name']='jy_robshop';
			pdo_insert('jy_weishop_game',$data);
		}
		exi('操作成功！','','edit');
	}
	$item=pdo_get('jy_weishop_game',array('action'=>'jy_robshop/game/','plugin_name'=>'jy_robshop','uniacid'=>$_SESSION['uniacid']));
	$item['pluginset']=$item['pluginset']?json_decode($item['pluginset'],true):array('rewardtype'=>0);
	$item['addtimes']=$item['addtimes']?json_decode($item['addtimes'],true):array();
	$item['red_shareset']=$item['pluginset']['red_shareset']?:array();
	$item['reward']=$item['reward']?json_decode($item['reward'],true):array();
	$item['sendtitle']=$item['sendtitle']?$item['sendtitle']:'[nickname]:恭喜您，获得一个参与奖励活动机会';
	$item['sendimage']=$item['sendimage']?$item['sendimage']:'../core/resource/images/h5game/bigwheel.png';
	$item['senddesc']=$item['senddesc']?$item['senddesc']:'活动有时间限制，请及时参与！';
	$isfenxiao=$this->plugin_exists('jy_commission');
	if($isfenxiao){
		$item['isfenxiao']=1;
		$return['fenxiao_levels']=pdo_getall('jy_commission_level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
		$return['fenxiao_levels']['0']=array('id'=>0,'title'=>'默认等级');
	}else{
		unset($return['addtimes_types'][8]);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基础设置',
				'list'=>array(
					array('type'=>'tip','title'=>'[活动短链接]:可以用于小程序，和菜单设置链接；
		</br>1、奖励实物[   ]元说明：举例设置奖励实物【10000】元，那么等于消耗【各等级奖金池累计金额】的10000元；
		</br>2、奖励[   ]个兑换券说明：举例设置奖金池1元等于【10】个兑换券，那么等于消耗【各等级奖金池累计金额】的1元；
		'),
					array('name'=>'title','title'=>'活动标题','type'=>'string'),
					array('name'=>'pluginset.total','title'=>'活动总人次限制','type'=>'string'),
					array('name'=>'pluginset.red_min','title'=>'活动介绍','type'=>'text'),
					array('type'=>'string','name'=>'sendtitle','title'=>'推送标题','help'=>'变量 [nickname] 会员昵称'),
					array('type'=>'image','name'=>'sendimage','title'=>'推送封面'),
					array('type'=>'string','name'=>'senddesc','title'=>'推送说明','help'=>'变量 [nickname] 会员昵称'),	
					array('name'=>'status','title'=>'是否开启','type'=>'switch'),
			)),
			array(
				'title'=>'大转盘参数设置',
				'list'=>array(
					array('name'=>'type','title'=>'奖励条件','type'=>'radio','options'=>'addtimes_types','help'=>''),
					//array('name'=>'pluginset.fenxiao_level','title'=>'指定分销商等级','type'=>'select','options'=>'fenxiao_levels','show'=>array('type'=>8)),
					//array('name'=>'goodsid','title'=>'选择商品','type'=>'choose','help'=>'','action'=>'jy_weishop/goods.get_goods/','show'=>array('type'=>2)),
					//array('name'=>'price','title'=>'最低订单金额','type'=>'string','help'=>'','show'=>array('type'=>1)),
					array('type'=>'unitstring','unit'=>'元','title'=>'抢中N元倍数得抽奖机会','name'=>'price','show'=>array('type'=>11),'help'=>'举例设置抢中100元的倍数为【抽奖机会】，如果会员抢中500元，那么就会抽中5次【抽奖机会】；'),
					array('type'=>'unitstring','unit'=>'元','title'=>'抢中N元倍数得指定中奖【再来一次】','name'=>'pluginset.zprice','show'=>array('type'=>11),'help'=>'相当于可以多玩一次，但中奖肯定是再来一次；举例设置抢中100元的倍数为【再来一次】，如果会员抢中500元，那么就会抽中5次【再来一次】；'),
					//array('type'=>'unitstring','unit'=>'个','title'=>'每次抽奖消耗N个积分','name'=>'delredfee','show'=>array('type'=>10),'help'=>'消耗的是，客户的积分'),
					array('type'=>'unitstring','unit'=>'次','title'=>'每日限制抽奖次数','name'=>'delredtimes','show'=>array('type'=>10),'help'=>'每天最多能用积分抽奖多少次？'),
					//array('name'=>'time','title'=>'弹窗时机','type'=>'radio','options'=>'times','show'=>array('type !='=>3),'help'=>'弹窗，提醒参与活动的时机！'),
					array('name'=>'pluginset.credit4_rate','title'=>'兑换券价值比例设置:1元等于[N]个兑换券','type'=>'unitstring','unit'=>'个'),
					array('type'=>'tip','title'=>'[<red>温馨提示</red>]：奖品的图片必须是正方形，否则一些地方显示会异常！
					<br>1、如果已中数量，已中金额大于等于奖品总数量/红包总金额，会提示库存不足！
					<br>2、如果所有有概率的，库存都不足，就会显示库存不足（概率0的库存再多也没用）。
					</br>公式一：抢中的金额 × 等级奖金池百分比=等级奖金池金额；
					</br>公式二：等级奖金池金额再按1元等于多少个兑换券数量；
					</br>公式三：当奖金池金额等于兑换券数量时，此时有人抽奖即开出这个【奖品等级-奖品类型的兑换券数量】；
					'),
					array(
					'type'=>'array',
					'name'=>'reward',
					'add_tip'=>'添加一个奖项',
						'list'=>array(
							array('type'=>'string','title'=>'奖品等级','name'=>'level'),
							array('type'=>'string','title'=>'奖品名称','name'=>'title'),
							array('type'=>'radio','title'=>'奖品类型','name'=>'type','help'=>'','options'=>'prizeType'),
							array('type'=>'unitstring','unit'=>'%','title'=>'奖金池%','name'=>'redfee_rate'),
							array('type'=>'string','title'=>'奖励数量','name'=>'jifen','show'=>array('type'=>array(2,3,4,5,6))),
							array('type'=>'unitstring','unit'=>'元','title'=>'商品价值','name'=>'goods_price','show'=>array('type'=>1)),
							//array('type'=>'string','title'=>'奖品总数量','name'=>'num'),
							//array('type'=>'string','title'=>'已中数量','name'=>'isgetnum'),
							array('type'=>'image','title'=>'奖品图片','name'=>'image','help'=>'温馨提示：<red>建议正方形图片</red>，否则有些游戏会显示异常！'),
						),
							'details'=>array(
								array('type'=>'string','title'=>'奖品等级','name'=>'level'),
								array('type'=>'string','title'=>'奖品名称','name'=>'title'),
								array('type'=>'radio','title'=>'奖品类型','name'=>'type','help'=>'支付参数--虚拟币--开启更多币种奖励','options'=>'prizeType'),
								array('type'=>'unitstring','unit'=>'%','title'=>'奖金池%','name'=>'redfee_rate'),
								array('type'=>'string','title'=>'奖励数量','name'=>'jifen','show'=>array('type'=>array(2,3,4,5,6))),
								array('type'=>'unitstring','unit'=>'元','title'=>'商品价值','name'=>'goods_price','show'=>array('type'=>1)),
								//array('type'=>'string','title'=>'奖品总数量','name'=>'num'),
								array('type'=>'image','title'=>'奖品图片','name'=>'image','help'=>'温馨提示：<red>建议正方形图片</red>，否则有些游戏会显示异常！'),
								
							)
					),
			)),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_game',array('id'=>$id));	
	}
	if(!$id){
		exi('请选择活动删除！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_game',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
